package com.xingkeduo.business.user.domain;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.user.service.*;
import com.xingkeduo.pojo.dto.*;
import com.xingkeduo.pojo.enums.DutyEnums;
import com.xingkeduo.pojo.query.*;
import com.xingkeduo.pojo.table.*;
import com.xingkeduo.pojo.table.JobseekerAssessTO.Type;
import com.xingkeduo.pojo.utils.JsonUtils;
import com.xingkeduo.util.pojo.base.BaseTable;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.BusinessException;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 信息管理业务
 *
 * @author 高杰
 * @time 2018年12月28日 下午5:30:18
 * @description
 */
@Component
public class ManageInfoDomain {

    private static Logger log = LoggerFactory.getLogger(ManageInfoDomain.class);

    @Autowired
    private JobseekerService jobseekerService;
    @Autowired
    private JobseekerSocialService jobseekerSocialService;
    @Autowired
    private JobseekerJobService jobseekerJobService;
    @Autowired
    private JobseekerJobDetailService jobseekerJobDetailService;
    @Autowired
    private JobseekerAssessService jobseekerAssessService;
    @Autowired
    private CraftsmanService craftsmanService;
    @Autowired
    private CraftsmanJobService craftsmanJobService;
    @Autowired
    private CraftsmanShopService craftsmanShopService;
    @Autowired
    private CraftsmanSocialService craftsmanSocialService;
    @Autowired
    private CraftsmanLevelService craftsmanLevelService;
    @Autowired
    private CraftsmanDutyService craftsmanDutyService;
    @Autowired
    private CraftsmanBackService craftsmanBackService;
    @Autowired
    private CraftsmanLeavingService craftsmanLeavingService;
    @Autowired
    private CraftsmanFileService craftsmanFileService;
    @Autowired
    private FeignDomain feignDomain;
    @Autowired
    private CraftsmanServiceService craftsmanServiceService;
    @Autowired
    private CraftsmanHealthService healthService;

    private static int initListSize = 15;
    private static int initMapSize = 16;

    /**
     * 获取发型师配置的服务类型信息 pantong  2019年5月18日上午11:49:09
     *
     * @param craftsmanServiceQO
     * @return
     * @throws Throwable List<ShopServiceDTO>
     */
    public Map<Long, List<CraftsmanServiceTO>> getUserServiceConfig(CraftsmanServiceQO craftsmanServiceQO) throws Throwable {
        Map<Long, List<CraftsmanServiceTO>> map = new HashMap<Long, List<CraftsmanServiceTO>>();
        List<CraftsmanServiceTO> list = craftsmanServiceService.selectByQuery(craftsmanServiceQO.setIsDelete(YesNoEnum.NO).setStatus(YesNoEnum.NO));
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) != null && map.get(list.get(i).getShopId()) != null && map.get(list.get(i).getShopId()).size() > 0) {
                    map.get(list.get(i).getShopId()).add(list.get(i));
                } else {
                    List<CraftsmanServiceTO> ll = new ArrayList<CraftsmanServiceTO>();
                    ll.add(list.get(i));
                    map.put(list.get(i).getShopId(), ll);
                }
            }
        }
        return map;
    }

    /**
     * 获取发型师配置的店铺信息 pantong  2019年5月18日上午11:49:09
     *
     * @param craftsmanServiceQO
     * @return
     * @throws Throwable List<ShopServiceDTO>
     */
    public List<CraftsmanShopTO> getCraftsmanShops(CraftsmanShopQO craftsmanShopQO) throws Throwable {
        return craftsmanShopService.selectByQuery(craftsmanShopQO.setIsDelete(YesNoEnum.NO));
    }

    /**
     * 获取求职者详细信息
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月3日 上午11:13:32
     */
    public JobseekerTO getJobseekerWithDetail(JobseekerQO jobseekerQuery) throws Throwable {
        JobseekerTO jobseekerTable = jobseekerService.selectByPrimary(jobseekerQuery.getId());
        if (jobseekerTable == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "信息不存在");

        jobseekerTable.setSocial(jobseekerSocialService.selectByPrimary(jobseekerQuery.getId()));
        jobseekerTable.setJob(getJobseekerJobWithDetails(new JobseekerJobQO().setId(jobseekerQuery.getId())));
        jobseekerTable.setAssesses(jobseekerAssessService.selectByLogin(jobseekerTable.getLogin()));


        return jobseekerTable;
    }

    /**
     * 获取求职者工作信息
     *
     * @param jobseekerJobQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午5:50:25
     */
    public JobseekerJobTO getJobseekerJob(JobseekerJobQO jobseekerJobQuery) throws Throwable {
        JobseekerTO jobseekerTable = jobseekerService.selectByPrimary(jobseekerJobQuery.getId());
        if (jobseekerTable == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "信息不存在");

        return jobseekerJobService.selectByPrimary(jobseekerJobQuery.getId());
    }

    /**
     * 获取求职者工作信息详情
     *
     * @param jobseekerJobQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午6:14:32
     */
    public JobseekerJobTO getJobseekerJobWithDetails(JobseekerJobQO jobseekerJobQuery) throws Throwable {
        JobseekerJobTO jobseekerJob = jobseekerJobService.selectByPrimary(jobseekerJobQuery.getId());
        if (jobseekerJob != null)
            jobseekerJob.setDetails(jobseekerJobDetailService.selectByJobseekerId(jobseekerJobQuery.getId()));
        return jobseekerJob;
    }

    /**
     * 获取求职者个人信息
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月3日 上午11:13:32
     */
    public JobseekerSocialTO getJobseekerSocial(JobseekerSocialQO jobseekerSocialQuery) throws Throwable {
        JobseekerTO jobseekerTable = jobseekerService.selectByPrimary(jobseekerSocialQuery.getId());
        if (jobseekerTable == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "信息不存在");

        return jobseekerSocialService.selectByPrimary(jobseekerSocialQuery.getId());
    }

    /**
     * 获取求职者信息列表
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午5:50:25
     */
    public List<JobseekerTO> getJobseekersWithoutRate(JobseekerQO jobseekerQO, JobseekerJobQO jobseekerJobQO,
                                                      JobseekerSocialQO jobseekerSocialQO, Page<JobseekerTO> page) throws Throwable {
        List<JobseekerTO> jobseekerTOs = getJobseekers(jobseekerQO, jobseekerJobQO, jobseekerSocialQO, page);
        if (!CommonUtil.isEmpty(jobseekerTOs))
            for (Iterator<JobseekerTO> iterator = jobseekerTOs.iterator(); iterator.hasNext(); ) {
                JobseekerTO jobseekerTO = iterator.next();
                jobseekerTO.setJob(jobseekerJobService.selectByPrimary(jobseekerTO.getId()));
                jobseekerTO.setSocial(jobseekerSocialService.selectByPrimary(jobseekerTO.getId()));
            }

        return jobseekerTOs;
    }

    /**
     * 人才库新增评分查询
     *
     * @param :
     * @return :
     * @author : gaozifeng
     * @date : 2019/8/7 15:27
     */
    public List<JobseekerTO> getJobseekersWithoutRateFail(JobseekerQO jobseekerQO, JobseekerJobQO jobseekerJobQO,
                                                          JobseekerSocialQO jobseekerSocialQO, JobseekerAssessRateQO jobseekerAssessRateQO, Page<JobseekerTO> page) throws Throwable {
        List<JobseekerTO> jobseekerTOs = getJobseekers(jobseekerQO, jobseekerJobQO, jobseekerSocialQO, page);
        List<JobseekerTO> returnList = new LinkedList<>();
        if (!CommonUtil.isEmpty(jobseekerTOs))
            for (Iterator<JobseekerTO> iterator = jobseekerTOs.iterator(); iterator.hasNext(); ) {
                JobseekerTO jobseekerTO = iterator.next();
                //新增评分
                jobseekerTO.setAssessList(jobseekerAssessService.selectByLogin(jobseekerTO.getLogin()));
                if (jobseekerTO.getAssessList().size() != 0) {
                    Map<String, Integer> map = new HashMap<>(8);
                    if (jobseekerAssessRateQO.getTypeSet().size() > 1) {
                        multipleConditionQuery(jobseekerTO, jobseekerAssessRateQO, map);
                    } else {
                        jobseekerTO.setJobseekerScore(initScroeMap(jobseekerTO.getAssessList(), jobseekerAssessRateQO, map));
                    }
                    if (null != jobseekerTO.getJobseekerScore() && jobseekerTO.getJobseekerScore().size() > 0) {
                        jobseekerTO.setJob(jobseekerJobService.selectByPrimary(jobseekerTO.getId()));
                        jobseekerTO.setSocial(jobseekerSocialService.selectByPrimary(jobseekerTO.getId()));
                        returnList.add(jobseekerTO);
                    }
                }
            }
        return returnList;
    }

    /**
     * 多条件评分搜索
     *
     * @param jobseekerTO:
     * @param jobseekerAssessRateQO:
     * @param map:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/8/12 17:48
     */
    private void multipleConditionQuery(JobseekerTO jobseekerTO, JobseekerAssessRateQO jobseekerAssessRateQO, Map<String, Integer> map) throws Throwable {
        initJobseekerAssessRateTypeSet(jobseekerAssessRateQO);
        //走mapper
        List<Long> assessIds = jobseekerAssessService.selectRateListByTypeAndScoreIds(jobseekerTO.getLogin());
        List<Long> list = jobseekerAssessService.selectUnionAll(jobseekerAssessRateQO, assessIds);
        //查询出评分id 查询评分表 处理成Map
        if (list.size() == 0) {
            return;
        }
        List<JobseekerAssessRateTO> rateList = jobseekerAssessService.selectQuery(list);
        //封装Map
        checkTEST1(rateList, map);
        checkTEST2(rateList, map);
        if (map.size() == jobseekerAssessRateQO.getTypeSet().size()) {
            jobseekerTO.setJobseekerScore(map);
        } else {
            return;
        }
    }

    private void initJobseekerAssessRateTypeSet(JobseekerAssessRateQO jobseekerAssessRateQO) {
        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);
        jobseekerAssessRateQO.setTypeSet(set);
    }

    /**
     * 初始化分数Map
     *
     * @param assessList:
     * @param jobseekerAssessRateQO:
     * @return : java.util.Map<java.lang.String,java.lang.Integer>
     * @author : gaozifeng
     * @date : 2019/8/12 17:37
     */
    private Map<String, Integer> initScroeMap(List<JobseekerAssessTO> assessList, JobseekerAssessRateQO jobseekerAssessRateQO, Map<String, Integer> map) throws Throwable {
        List<JobseekerAssessRateTO> rateList = jobseekerAssessService.rateList(assessList);
        Boolean flag = checkCondition(rateList, jobseekerAssessRateQO);
        if (!CommonUtil.isEmpty(rateList) && flag) {
            //初试
            checkTEST1(rateList, map);
            //复试
            checkTEST2(rateList, map);
        }
        return map;
    }

    /**
     * 检查评分类型和分数是否可以返回数据
     *
     * @param rateList:
     * @return : java.lang.Boolean
     * @author : gaozifeng
     * @date : 2019/8/8 16:32
     */
    private Boolean checkCondition(List<JobseekerAssessRateTO> rateList, JobseekerAssessRateQO jobseekerAssessRateQO) {
        Boolean flag = false;
        //提取过滤评分
        Set<Integer> scoreList = jobseekerAssessRateQO.getScoreList();
        //提取type
        Set<Integer> typeSet = jobseekerAssessRateQO.getTypeSet();
        for (JobseekerAssessRateTO rate : rateList) {
            if (typeSet.size() > 0) {
                if (typeSet.contains(rate.getType().getValue())) {
                    if (scoreList.contains(rate.getScore())) {
                        flag = true;
                    }
                }
            } else {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 初始化初试评分
     *
     * @param rateList:
     * @param map:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/8/8 16:29
     */
    private void checkTEST1(List<JobseekerAssessRateTO> rateList, Map<String, Integer> map) {
        if (!CommonUtil.isEmpty(rateList)) {
            if (rateList.get(0).getType() == JobseekerAssessRateTO.Type.FIRST) {
                map.put("firstScore", rateList.get(0).getScore());
            }
        }
    }

    /**
     * 初始化复试中 技术、素养
     *
     * @param rateList:
     * @param map:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/8/8 16:29
     */
    private void checkTEST2(List<JobseekerAssessRateTO> rateList, Map<String, Integer> map) {
        if (!CommonUtil.isEmpty(rateList)) {
            for (JobseekerAssessRateTO rate : rateList) {
                if (rate.getType() == JobseekerAssessRateTO.Type.TECH) {
                    map.put("techScore", rate.getScore() == 0 ? 0 : rate.getScore());
                }
                if (rate.getType() == JobseekerAssessRateTO.Type.ATTAIN) {
                    map.put("attainScore", rate.getScore() == 0 ? 0 : rate.getScore());
                }
            }
        }
    }

    /**
     * 获取求职者信息列表
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午5:50:25
     */
    public List<JobseekerTO> getJobseekersWithRate(JobseekerQO jobseekerQO, JobseekerJobQO jobseekerJobQO,
                                                   JobseekerSocialQO jobseekerSocialQO, Page<JobseekerTO> page) throws Throwable {
        List<JobseekerTO> jobseekerTOs = getJobseekers(jobseekerQO, jobseekerJobQO, jobseekerSocialQO, page);
        if (!CommonUtil.isEmpty(jobseekerTOs))
            for (Iterator<JobseekerTO> iterator = jobseekerTOs.iterator(); iterator.hasNext(); ) {
                JobseekerTO jobseekerTO = iterator.next();
                jobseekerTO.setJob(jobseekerJobService.selectByPrimary(jobseekerTO.getId()));
                jobseekerTO.setSocial(jobseekerSocialService.selectByPrimary(jobseekerTO.getId()));
                jobseekerTO.setLastFirstExam(jobseekerAssessService
                        .selectFirstExamByJobseekerIdAndTop(JobseekerAssessTO.Type.TEST1, jobseekerTO.getId(), 1));
                jobseekerTO.setLastSecondExam(jobseekerAssessService
                        .selectFirstExamByJobseekerIdAndTop(JobseekerAssessTO.Type.TEST2, jobseekerTO.getId(), 1));
            }

        return jobseekerTOs;
    }

    /**
     * 获取求职者信息列表
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午5:50:25
     */
    public List<JobseekerTO> getJobseekers(JobseekerQO jobseekerQO, JobseekerJobQO jobseekerJobQO,
                                           JobseekerSocialQO jobseekerSocialQO, Page<JobseekerTO> page) throws Throwable {
        List<Long> jobIds = null;
        if (!jobseekerJobQO.isEmpty()) {
            jobIds = jobseekerJobService.selectPrimaryByQuery(jobseekerJobQO);
            if (CommonUtil.isEmpty(jobIds))
                return Collections.emptyList();
        }

        List<Long> socialIds = null;
        if (!jobseekerSocialQO.isEmpty()) {
            socialIds = jobseekerSocialService.selectPrimaryByQuery(jobseekerSocialQO);
            if (CommonUtil.isEmpty(socialIds))
                return Collections.emptyList();
        }

        List<Long> ids = new ArrayList<>();
        if (jobIds != null)
            ids.addAll(jobIds);
        if (socialIds != null) {
            if (ids.size() > 0) {
                ids.retainAll(socialIds);
                if (ids.size() <= 0) {
                    return Collections.emptyList();
                }
            } else
                ids.addAll(socialIds);
        }

        jobseekerQO.setIds(new HashSet<>(ids));
        page.init(jobseekerService.count(jobseekerQO));
        jobseekerQO.setRecordStart(page.getRecordStart()).setRecordEnd(page.getRecordEnd());
        if (null != jobseekerQO.getStatuses() && jobseekerQO.getStatuses().size() > 0) {
            return jobseekerService.selectByQuery(jobseekerQO.setOrderBy("out_time DESC"));
        } else if (jobseekerQO.getStatus() == JobseekerTO.Status.TEST1) {
            return jobseekerService.selectByQuery(jobseekerQO.setOrderBy("createdtime DESC"));
        } else {
            return jobseekerService.selectByQuery(jobseekerQO.setOrderBy("updatedtime DESC"));
        }
    }

    /**
     * 获取求职者工作信息列表
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月29日 下午5:50:25
     */
    public List<CraftsmanTO> getCraftsmans(CraftsmanQO craftsmanQuery, CraftsmanJobQO craftsmanJobQuery,
                                           CraftsmanSocialQO craftsmanSocialQuery, Page<CraftsmanTO> page) throws Throwable {
        // TODO 根据城市和分公司，查询店铺ID集合

        List<Long> jobIds = craftsmanJobService.selectPrimatyByQuery(craftsmanJobQuery);
        List<Long> socialIds = craftsmanSocialService.selectPrimaryByQuery(craftsmanSocialQuery);
        if (!CommonUtil.isEmpty(jobIds) && !CommonUtil.isEmpty(socialIds)) {
            craftsmanQuery.setIds(new ArrayList<>());
            if (!CommonUtil.isEmpty(jobIds))
                craftsmanQuery.getIds().addAll(jobIds);
            if (!CommonUtil.isEmpty(socialIds))
                craftsmanQuery.getIds().addAll(socialIds);
        }

        List<CraftsmanTO> craftsmans = craftsmanService.selectByQuery(craftsmanQuery);
        if (!CommonUtil.isEmpty(craftsmans))
            for (Iterator<CraftsmanTO> iterator = craftsmans.iterator(); iterator.hasNext(); ) {
                CraftsmanTO craftsman = iterator.next();
                craftsman.setJob(craftsmanJobService.selectByPrimary(craftsman.getId()));
                craftsman.setSocial(craftsmanSocialService.selectByPrimary(craftsman.getId()));
            }
        page.init(craftsmanService.count(craftsmanQuery));
        craftsmanQuery.setRecordStart(page.getRecordStart()).setRecordEnd(page.getRecordEnd());
        return craftsmans;
    }


    /**
     * 获取求职者工作信息列表 分页
     *
     * @param craftsmanQuery:
     * @param craftsmanJobQuery:
     * @param craftsmanSocialQuery:
     * @param page:
     * @return : java.util.List<com.xingkeduo.pojo.table.CraftsmanTO>
     * @author : gaozifeng
     * @date : 2019/3/4 11:10
     */
    public List<CraftsmanTO> getCraftsmansPage(CraftsmanQO craftsmanQuery, CraftsmanJobQO craftsmanJobQuery, CraftsmanServiceQO craftsmanServiceQuery,
                                               CraftsmanSocialQO craftsmanSocialQuery, Page<CraftsmanTO> page) throws Throwable {

        //根据craftsmanServiceQuery查询数据
        List<Long> serviceIds = null;
        if (!craftsmanServiceQuery.isEmpty()) {
            List<CraftsmanServiceTO> seviceList = craftsmanServiceService.selectByQuery(craftsmanServiceQuery.setIsDelete(YesNoEnum.NO));
            if (seviceList != null && seviceList.size() > 0) {
                serviceIds = new ArrayList<Long>();
                for (int i = 0; i < seviceList.size(); i++) {
                    if (seviceList.get(i) != null && seviceList.get(i).getCraftsmanId() > 0) {
                        serviceIds.add(seviceList.get(i).getCraftsmanId());
                    }
                }
            }
            if (CommonUtil.isEmpty(serviceIds))
                return Collections.emptyList();
        }

        //判断昵称是否为空 不为空 用昵称查询获取jobids1
        List<Long> jobids_nickname = null;
        CraftsmanJobQO craftsmanJobQO = new CraftsmanJobQO();
        if (StringUtils.isNotEmpty(craftsmanJobQuery.getNickName())) {
            craftsmanJobQO.setNickName(craftsmanJobQuery.getNickName()).setIsDelete(YesNoEnum.NO);
            jobids_nickname = craftsmanJobService.selectPrimatyByQuery(craftsmanJobQO);
        }

        //排除昵称条件 其他条件查询jobIds
        List<Long> jobIds = null;
        craftsmanJobQuery.setNickName(null);
        if (!craftsmanJobQuery.isEmpty()) {
            jobIds = craftsmanJobService.selectPrimatyByQuery(craftsmanJobQuery.setIsDelete(YesNoEnum.NO));
            if (CommonUtil.isEmpty(jobIds))
                return Collections.emptyList();
        }

        //判断姓名手机号是否为空  不为空 查询对应的ids
        List<Long> jobids_condition = null;
        CraftsmanSocialQO craftsmanSocialQO = new CraftsmanSocialQO();
        if (StringUtils.isNotEmpty(craftsmanSocialQuery.getCondition())) {
            craftsmanSocialQO.setIsDelete(YesNoEnum.NO).setCondition(craftsmanSocialQuery.getCondition());
            jobids_condition = craftsmanSocialService.selectPrimaryByQuery(craftsmanSocialQO);
        }

        //排除condition 其他条件查询socialIds
        List<Long> socialIds = null;
        craftsmanSocialQuery.setCondition(null);
        if (!craftsmanSocialQuery.isEmpty()) {
            socialIds = craftsmanSocialService.selectPrimaryByQuery(craftsmanSocialQuery.setIsDelete(YesNoEnum.NO));
            if (CommonUtil.isEmpty(socialIds))
                return Collections.emptyList();
        }

        //name 手机号获取到的id集合
        List<Long> nameIds = new ArrayList<Long>();
        if (StringUtils.isNotEmpty(craftsmanJobQO.getNickName()) || StringUtils.isNotEmpty(craftsmanSocialQO.getCondition())) {//此时如果结果为空则返回empty
            if ((jobids_nickname == null || jobids_nickname.size() <= 0) && (jobids_condition == null || jobids_condition.size() <= 0)) {
                return Collections.emptyList();
            }
            if (jobids_nickname != null && jobids_nickname.size() > 0) {
                nameIds.addAll(jobids_nickname);
            }
            if (jobids_condition != null && jobids_condition.size() > 0) {
                nameIds.addAll(jobids_condition);
            }
            if (CommonUtil.isEmpty(nameIds)) {
                return Collections.emptyList();
            }
        }

        //jobids  socialIds  nameIds取交集
        List<Long> ids = new ArrayList<>();
        if (jobIds != null)
            ids.addAll(jobIds);
        if (socialIds != null) {
            if (ids.size() > 0) {
                ids.retainAll(socialIds);
                if (ids.size() <= 0) {
                    return Collections.emptyList();
                }
            } else
                ids.addAll(socialIds);
        }
        if (serviceIds != null && serviceIds.size() > 0) {
            if (ids.size() > 0) {
                ids.retainAll(serviceIds);
                if (ids.size() <= 0) {
                    return Collections.emptyList();
                }
            } else
                ids.addAll(serviceIds);
        }
        if (nameIds != null && nameIds.size() > 0) {
            if (ids.size() > 0) {
                ids.retainAll(nameIds);
                if (ids.size() <= 0) {
                    return Collections.emptyList();
                }
            } else
                ids.addAll(nameIds);
        }

        craftsmanQuery.setIds(ids);

        page.init(craftsmanService.count(craftsmanQuery.setIsDelete(YesNoEnum.NO)));
        craftsmanQuery.setRecordStart(page.getRecordStart()).setRecordEnd(page.getRecordEnd());

        //需要先查出job 所有表,然后查出 craftsman里面前10个 时间倒叙。
        if (null != craftsmanQuery.getStatus()) {
            switch (craftsmanQuery.getStatus()) {
                case STUDENT:
                    craftsmanJobQuery.setOrderBy("intime DESC").setInTime("1");
                    break;
                case FLOW:
                    craftsmanJobQuery.setOrderBy("gradtime DESC").setGradTime("1");
                    break;
                case BIND:
                    craftsmanJobQuery.setOrderBy("formaltime DESC").setFormalTime("1");
                    break;
            }
        } else if (null != craftsmanQuery.getStatuss()) {
            craftsmanJobQuery.setOrderBy("leavetime DESC").setLeaveTime("1");
        }
        List<Long> jobList;
        if (craftsmanQuery.getIds().size() > 0) {
            //有查询条件
            jobList = craftsmanJobService.selectPrimatyByQuery(craftsmanJobQuery.setIds(craftsmanQuery.getIds()).setIsDelete(YesNoEnum.NO));
        } else {
            //没有查询条件
            jobList = craftsmanJobService.selectPrimatyByQuery(craftsmanJobQuery.setIsDelete(YesNoEnum.NO));
        }

        List<Long> craftsmanListIds;
        if (jobList.size() > 0) {
            craftsmanListIds = craftsmanService.selectById(craftsmanQuery.setIds(jobList).setIsDelete(YesNoEnum.NO));
        } else {
            craftsmanListIds = craftsmanService.selectById(craftsmanQuery.setIsDelete(YesNoEnum.NO));
        }
        if (null == craftsmanListIds || craftsmanListIds.size() == 0) {
            return Collections.emptyList();
        }
        List<CraftsmanTO> returnList = null;
        List<CraftsmanTO> craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(craftsmanListIds));
        Map<Long, CraftsmanTO> craftsmanMap = craftsmanList.stream().collect(Collectors.toMap(CraftsmanTO::getId, a -> a, (k1, k2) -> k1));

        List<CraftsmanJobTO> jobTOList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setIds(craftsmanListIds).setIsDelete(YesNoEnum.NO));
        Map<Long, CraftsmanJobTO> jobMap = jobTOList.stream().collect(Collectors.toMap(CraftsmanJobTO::getId, a -> a, (k1, k2) -> k1));

        List<CraftsmanSocialTO> socialList = null;
        if (null != craftsmanListIds && craftsmanListIds.size() > 0) {
            CraftsmanSocialQO socialQO = new CraftsmanSocialQO();
            socialQO.setIds(craftsmanListIds).setIsDelete(YesNoEnum.NO);
            socialList = craftsmanSocialService.selectByQuery(socialQO);
        } else {
            socialList = craftsmanSocialService.selectByQuery(craftsmanSocialQO.setIds(craftsmanListIds).setIsDelete(YesNoEnum.NO));
        }
        Map<Long, CraftsmanSocialTO> socialMap = socialList.stream().collect(Collectors.toMap(CraftsmanSocialTO::getId, a -> a, (k1, k2) -> k1));

        //jobseeker记录
        List<String> loginList = craftsmanList.stream().filter(a -> null != a.getLogin()).map(a -> a.getLogin()).collect(Collectors.toList());
        Map<Long, JobseekerJobTO> jobseekerJobMap = null;
        Map<String, JobseekerTO> jobseekerMap = null;
        if (null != loginList && loginList.size() > 0) {
            List<JobseekerTO> jobseekerList = jobseekerService.selectByQuery(new JobseekerQO().setLogins(loginList).setIsDelete(YesNoEnum.NO));
            jobseekerMap = jobseekerList.stream().collect(Collectors.toMap(JobseekerTO::getLogin, a -> a, (k1, k2) -> k1));
            //jobseekerJob记录
            List<Long> jobListIds = jobseekerList.stream().map(a -> a.getId()).collect(Collectors.toList());
            List<JobseekerJobTO> jobseekerJobList = jobseekerJobService.selectQuery(new JobseekerJobQO().setIds(jobListIds));
            jobseekerJobMap = jobseekerJobList.stream().collect(Collectors.toMap(JobseekerJobTO::getId, a -> a, (k1, k2) -> k1));
        }

        //岗位信息
        List<CraftsmanDutyTO> dutyList = craftsmanDutyService.selectByQuery(new CraftsmanDutyQO());
        Map<Long, CraftsmanDutyTO> dutyMap = dutyList.stream().collect(Collectors.toMap(CraftsmanDutyTO::getId, a -> a, (k1, k2) -> k1));

        if (null != craftsmanList && craftsmanList.size() > 0) {
            returnList = new LinkedList<>();

            for (int i = 0, j = craftsmanList.size(); i < j; i++) {
                CraftsmanTO manTO = craftsmanMap.get(craftsmanList.get(i).getId());
                CraftsmanJobTO jobTO = jobMap.get(manTO.getId());
                manTO.setJob(null == jobTO ? null : jobTO);
                manTO.setSocial(socialMap.get(jobTO.getId()));
                if (StringUtils.isNotEmpty(manTO.getLogin()) && null != jobseekerMap) {
                    JobseekerTO jobseekerTO = jobseekerMap.get(manTO.getLogin());
                    if (null != jobseekerTO) {
                        manTO.setJobseekerJobTO(jobseekerJobMap.get(jobseekerTO.getId()));
                    }
                }
                if (null != manTO.getJob() && manTO.getJob().getDutyId() > 0) {
                    manTO.setDuty(dutyMap.get(manTO.getJob().getDutyId()));
                }
                //获取发型师对应的servicelist
                CraftsmanServiceQO serviceQQ = new CraftsmanServiceQO();
                List<Long> shopIds = new ArrayList<Long>();
                shopIds.add(0L);
                serviceQQ.setCraftsmanId(craftsmanList.get(i).getId()).setShopIds(shopIds);
                List<CraftsmanServiceTO> seviceList = craftsmanServiceService.selectByQuery(serviceQQ.setIsDelete(YesNoEnum.NO));
                manTO.setCraftsmanServiceList(seviceList);
                returnList.add(manTO);
            }
        }
        page.setDtoList(returnList);
        return returnList;
    }

    /**
     * 获取一线员工详情
     *
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:40:26
     */
    public CraftsmanTO getCraftsmanWithDetail(CraftsmanQO craftsmanQuery) throws Throwable {
        CraftsmanTO craftsman = craftsmanService.selectByPrimary(craftsmanQuery.getId());
        if (craftsman == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "信息不存在");

        craftsman.setJob(craftsmanJobService.selectByPrimary(craftsmanQuery.getId()));
        craftsman.setSocial(craftsmanSocialService.selectByPrimary(craftsmanQuery.getId()));
//        List<JobseekerAssessTO> jobseekerAssessTO = jobseekerAssessService.selectByLogin(craftsman.getLogin());
//            craftsman.setAssesses(jobseekerAssessTO);

        //跟踪表 先删除
//        craftsman.setTraces(craftsmanTraceService.selectByLogin(craftsman.getLogin()));
        if (craftsman.getStatus() == CraftsmanTO.Status.LEAVE)
            craftsman.setLeaving(craftsmanLeavingService.selectLastByCraftsmanId(craftsman.getId()));

        if (null != craftsman.getJob() && craftsman.getJob().getLevelId() > 0) {
            craftsman.setCraftsmanLevelTO(craftsmanLevelService.selectByPrimary(craftsman.getJob().getLevelId()));
        }
        if (null != craftsman.getJob() && craftsman.getJob().getDutyId() > 0) {
            craftsman.setDuty(craftsmanDutyService.selectByPrimary(craftsman.getJob().getDutyId()));
        }
        //只有总经理才有分公司
        List<CraftsmanDutyTO> craftsmanDutyTOS = craftsmanDutyService.selectByQuery(new CraftsmanDutyQO().setId(craftsman.getJob().getDutyId()));
        if (null != craftsmanDutyTOS && craftsmanDutyTOS.size() > 0) {
            if (craftsmanDutyTOS.get(0).getSequence() == 3) {
                if (0 != craftsman.getJob().getCompanyId()) {
                    StringBuilder sb = new StringBuilder("{");
                    sb.append("id:");
                    long companyId = craftsman.getJob().getCompanyId();
                    sb.append(companyId);
                    sb.append("}");
                    List<BaseDTO> company = feignDomain.getCompanyForList(sb.toString());
                    craftsman.setCompanyDTOList(company);
                }
            }
        }

        //只有发型师才有门店
        if (craftsman.getStatus() == CraftsmanTO.Status.BIND) {
            CraftsmanShopQO craftsmanShopQO = new CraftsmanShopQO();
            craftsmanShopQO.setCraftsmanId(craftsman.getId());
            List<CraftsmanShopTO> craftsmanShopTOS = craftsmanShopService.selectByQuery(craftsmanShopQO);
            if (null != craftsmanShopTOS && craftsmanShopTOS.size() > 0) {
                String ids = JsonUtils.encodeParam("ids", craftsmanShopTOS.stream().map(a -> a.getShopId()).collect(Collectors.toList()));
                List<BaseDTO> shops = feignDomain.getShopForList(ids);
                craftsman.setShopList(shops);
                BaseDTO shop = feignDomain.getShopByKey(craftsmanShopTOS.get(0).getShopId());
                craftsman.setShopDTO(shop);
            }
            List<BaseDTO> shopForList = feignDomain.getShopForList("{}");
            craftsman.setShopList(shopForList);
        }

        //银行卡信息等
        Map<String, CraftsmanFileTO> map = new HashMap<>(initMapSize);
        List<CraftsmanFileTO> craftsmanFileTOList = craftsmanFileService.selectForList(new CraftsmanFileQO().setJobseeker_id(craftsman.getId()).setIsDelete(YesNoEnum.NO));
        if (craftsmanFileTOList.size() > 0) {
//            craftsman.setFiles(craftsmanFileTOList);
            craftsman.setPictureGrounp(craftsmanFileTOList);
        }
        //健康证信息
        List<CraftsmanHealthTO> health = healthService.selectByQuery(new CraftsmanHealthQO().setIsDelete(YesNoEnum.NO).setCraftsman_id(craftsman.getId()));
        if (health.size() > 0) {
            craftsman.setHealthTO(health.get(0));
//            craftsman.setHealthList(health);
        }

        //查询求职者的面试信息
        if (StringUtils.isNotEmpty(craftsman.getLogin())) {
            craftsman.setJobseekerTO(interviewInfo(craftsman.getLogin()));
        }
        return craftsman;
    }

    /**
     * 根据unionid 查询求职者面试信息
     *
     * @param login:
     * @return : com.xingkeduo.pojo.table.JobseekerTO
     * @author : gaozifeng
     * @date : 2019/5/25 14:32
     */
    private JobseekerTO interviewInfo(String login) throws Throwable {
        //面试信息
        JobseekerTO jobseekerTO = jobseekerService.selectByLogin(login);
        if (jobseekerTO == null) {
            return null;
        }
        //当前求职者信息
        JobseekerJobTO jobseekerJobTO = jobseekerJobService.selectByPrimary(jobseekerTO.getId());
        jobseekerTO.setJob(jobseekerJobTO);
        //工作经历
        List<JobseekerJobDetailTO> jobseekerJobDetailTOS = jobseekerJobDetailService.selectByJobseekerId(jobseekerTO.getId());
        jobseekerTO.setJobDetailList(jobseekerJobDetailTOS);
        List<JobseekerAssessTO> jobseekerAssessTOS = jobseekerAssessService.selectByLoginAndType(new JobseekerAssessQO().setLogin(login).setType(JobseekerAssessTO.Type.TEST3).setIsDelete(YesNoEnum.NO));
        //求职者培训信息   status=50
        if (null != jobseekerAssessTOS && jobseekerAssessTOS.size() > 0) {
            jobseekerTO.setAssessTO(jobseekerAssessTOS.get(0));
        }
        //求职者面试经历 初试 复试记录 都要
        Set<Type> set = new HashSet<>();
        set.add(Type.TEST1);
        set.add(Type.TEST2);
        List<JobseekerAssessTO> faceList = jobseekerAssessService.selectByLoginAndType(new JobseekerAssessQO().setLogin(jobseekerTO.getLogin()).setTypes(set).setIsDelete(YesNoEnum.NO).setOrderBy("createdtime DESC"));
        jobseekerTO.setAssessList(faceList);

        return jobseekerTO;
    }

    /**
     * 修改学员信息
     *
     * @param jobseekerSocialTO:
     * @param jobseekerJobTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/12 19:29
     */
    @Transactional
    public void modifyJobseekerDetail(JobseekerSocialTO jobseekerSocialTO, JobseekerJobTO jobseekerJobTO)
            throws Throwable {
        JobseekerTO jobseekerTO = jobseekerService.selectByPrimary(jobseekerSocialTO.getId());
        if (null == jobseekerTO)
            throw new BusinessException(BusinessException.Type.DEFAULT, "信息不存在");

        jobseekerJobService.update(jobseekerJobTO);
        jobseekerSocialService.update(jobseekerSocialTO);
    }

    /**
     * 修改一线员工详情
     *
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:40:26
     */
    @Transactional
    public void modifyCraftsmanDetail(CraftsmanTO craftsman, CraftsmanSocialTO craftsmanSocial,
                                      CraftsmanJobTO craftsmanJob, BaseDTO companyDTO, CraftsmanShopQO shopTO, long operator) throws Throwable {
//                                      CraftsmanJobTO craftsmanJob, BaseDTO companyDTO, CraftsmanShopTO shopTO, long operator) throws Throwable {
        CraftsmanTO craftsmanDb = craftsmanService.selectByPrimary(craftsman.getId());
        CraftsmanSocialTO socialTO = getCraftsmanSocialById(craftsmanDb.getId());
        if (null != craftsmanSocial.getPhone()) {
            if (!craftsmanSocial.getPhone().equals(socialTO.getPhone())) {
                List<CraftsmanSocialTO> craftsmanSocialTOS = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setPhone(craftsmanSocial.getPhone()).setIsDelete(YesNoEnum.NO));
                if (null != craftsmanSocialTOS && craftsmanSocialTOS.size() > 0) {
                    throw new BusinessException(BusinessException.Type.DEFAULT, "此手机号已经被其他发型师绑定");
                }
            }
        }
        craftsmanService.update(craftsman.setUpdatedBy(operator));
        craftsmanSocialService.update(craftsmanSocial.setUpdatedBy(operator));
        craftsmanJob.setUpdatedBy(operator);
        if (StringUtils.isNotEmpty(craftsmanDb.getLogin())) {//添加非空判断  -- 兼容老数据
            JobseekerTO jobseeker = jobseekerService.selectByLogin(craftsmanDb.getLogin());
            if (jobseeker != null) {//添加非空判断  -- 兼容老数据
                JobseekerSocialTO jobseekerSocialTO = jobseekerSocialService.selectByPrimary(jobseeker.getId());
                if (null != jobseekerSocialTO) {
                    jobseekerSocialService
                            .update(new JobseekerSocialTO().setUpdatedBy(operator).copy(craftsmanSocial).setId(jobseeker.getId()));
                }
            }
        }
        //把之前配置的总经理及门店经理的信息清除掉
        //清理总经理的分公司
        craftsmanJob.setCompanyId(0);
        //清理店经理的店铺
        List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setCraftsmanId(craftsmanDb.getId()).setIsDelete(YesNoEnum.NO));
        if (shopTOList != null && shopTOList.size() > 0) {
            List<CraftsmanShopTO> updateList = shopTOList.stream().map(a -> a.setIsDelete(YesNoEnum.YES)).collect(Collectors.toList());
            int retVal = craftsmanShopService.updateList(updateList);
            if (updateList.size() != retVal) {
                log.info("修改的店铺集合{}" + updateList.toString());
                log.info("修改的店铺集合size{}" + updateList.size());
                log.info("修改的店铺retVal" + retVal);
                throw new BusinessException(BusinessException.Type.DEFAULT, "修改店铺失败");
            }
        }

        if (StringUtils.isNotEmpty(companyDTO.getId())) {
            craftsmanJob.setCompanyId(Long.parseLong(companyDTO.getId()));
        }
        craftsmanJobService.update(craftsmanJob);

        if (shopTO.getShopId() > 0) {
            CraftsmanShopQO craftsmanShopQO = new CraftsmanShopQO();
            craftsmanShopQO.setCraftsmanId(craftsman.getId()).setIsDelete(YesNoEnum.NO);
            List<CraftsmanShopTO> craftsmanShopTOS = craftsmanShopService.selectByQuery(craftsmanShopQO);
            if (null != craftsmanShopTOS && craftsmanShopTOS.size() > 0) {
                CraftsmanShopTO shop = craftsmanShopTOS.get(0);
                shop.setShopId(shopTO.getShopId());
                craftsmanShopService.update(shop);
            }
        }

        if (shopTO.getShopIds() != null && shopTO.getShopIds().size() > 0) {
            List<Long> shopIds = shopTO.getShopIds();
            String ids = JsonUtils.encodeParam("ids", shopIds);
            List<CraftsmanShopTO> insertList = new ArrayList<CraftsmanShopTO>();
            for (int i = 0; i < shopTO.getShopIds().size(); i++) {
                CraftsmanShopTO shop = new CraftsmanShopTO();
                shop.setCraftsmanId(craftsman.getId());
                shop.setIsDelete(YesNoEnum.NO);
                shop.setId(IdentityUtil.nextId(Worker.USER));
                shop.setShopId(shopIds.get(i));
                shop.setCreatedBy(operator);
                shop.setUpdatedBy(operator);
                insertList.add(shop);
            }
            craftsmanShopService.insertList(insertList);
        }
    }

    /**
     * 一线员工离职
     *
     * @param craftsmanLeaving
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月18日 下午7:25:03
     */
    @Transactional
    public void modifyCraftsmanLeave(CraftsmanLeavingTO craftsmanLeaving, long operator) throws Throwable {
        log.info("员工离职Domain---------------" + craftsmanLeaving);
        CraftsmanTO craftsmanDb = craftsmanService.selectByPrimary(craftsmanLeaving.getCraftsmanId());
        if (craftsmanDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "员工不存在");
        if (craftsmanDb.getStatus() == CraftsmanTO.Status.LEAVE)
            throw new BusinessException(BusinessException.Type.DEFAULT, "员工已经离职");

        craftsmanDb.setLeaveStatus(craftsmanDb.getStatus());
        craftsmanDb.setStatus(CraftsmanTO.Status.LEAVE);
        craftsmanService.update(craftsmanDb.setUpdatedBy(operator));
        craftsmanLeaving.setIsDelete(YesNoEnum.NO);
        craftsmanLeavingService.insert(craftsmanLeaving);

        CraftsmanJobTO jobTO = craftsmanJobService.selectByPrimary(craftsmanDb.getId());
        jobTO.setLeaveTime(new Date());
        craftsmanJobService.update(jobTO);

        CraftsmanDutyTO dutyTO = craftsmanDutyService.selectByPrimary(jobTO.getDutyId());
        if (dutyTO.getSequence() == DutyEnums.MANAGER.getSequence()) {

            List<CraftsmanShopTO> shopList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setCraftsmanId(craftsmanDb.getId()).setIsDelete(YesNoEnum.NO));

            List<CraftsmanShopTO> updateList = shopList.stream().map(a -> a.setIsDelete(YesNoEnum.YES)).collect(Collectors.toList());
            int retVal = craftsmanShopService.updateList(updateList);
            if (retVal != updateList.size()) {
                log.info("user -------离职 经理身份 修改店铺失败");
                throw new BusinessException(BusinessException.Type.DEFAULT, "店经理 修改管理门店失败.");
            }
        }

        List<CraftsmanServiceTO> serviceTOList = craftsmanServiceService.selectByQuery(new CraftsmanServiceQO().setCraftsmanId(craftsmanDb.getId()).setIsDelete(YesNoEnum.NO).setShopId(-1L));
        if (null != serviceTOList && serviceTOList.size() > 0) {
            log.info("离职人员------修改服务技能 都是失效" + craftsmanDb.getId());
            List<CraftsmanServiceTO> updateList = serviceTOList.stream().map(a -> a.setStuatus(YesNoEnum.YES)).collect(Collectors.toList());
            int retVal = craftsmanServiceService.updateList(updateList);
            if (retVal != serviceTOList.size()) {
                log.info("离职人员操作，修改发型师调价 失败");
                throw new BusinessException(BusinessException.Type.DEFAULT, "修改调价信息失败");
            }
        }
    }

    /**
     * 复职发型师
     *
     * @param craftsmanQO: 发型师
     * @param backTO:      复职记录
     * @return : void
     * @author : gaozifeng
     * @date : 2019/4/26 16:55
     */
    @Transactional
    public void revertCraftsman(CraftsmanQO craftsmanQO, CraftsmanBackTO backTO, long updateId) throws Throwable {
        CraftsmanTO craftsmanDb = craftsmanService.selectByPrimary(craftsmanQO.getId());
        if (craftsmanDb == null) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "员工不存在");
        }
        if (CraftsmanTO.Status.LEAVE.getValue() != craftsmanDb.getStatus().getValue()) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师非离职状态");
        }
        if (CraftsmanTO.Status.LEAVE == craftsmanDb.getStatus()) {
            List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setCraftsmanId(craftsmanDb.getId()).setIsDelete(YesNoEnum.YES));
            if (null == shopTOList || shopTOList.size() <= 0) {
                //以后判断
//                throw new BusinessException(BusinessException.Type.DEFAULT, "发型师离职店铺不存在");
            } else {
//                craftsmanShopService.update(shopTOList.get(0).setIsDelete(YesNoEnum.NO).setUpdatedBy(updateId));
                List<CraftsmanLeavingTO> craftsmanLeavingTOS = craftsmanLeavingService.selectByQuery(new CraftsmanLeavingQO().setCraftsmanId(craftsmanDb.getId()).setIsDelete(YesNoEnum.NO));
                if (null != craftsmanLeavingTOS && craftsmanLeavingTOS.size() > 0) {
                    log.info("复职门店信息修改--------------:" + shopTOList.get(0).toString());
                    CraftsmanLeavingTO craftsmanLeavingTO = craftsmanLeavingTOS.get(0);
                    craftsmanLeavingTO.setIsDelete(YesNoEnum.YES).setUpdatedBy(updateId);
                    craftsmanLeavingService.update(craftsmanLeavingTO);
                }
            }
        }

        craftsmanDb.setStatus(craftsmanDb.getLeaveStatus());
        craftsmanService.update(craftsmanDb);
        backTO.setCraftsmanId(craftsmanDb.getId()).setCreatedBy(updateId).setIsDelete(YesNoEnum.NO).setId(IdentityUtil.nextId(Worker.USER));
        craftsmanBackService.insert(backTO);

    }


    /**
     * 修改一线员工店铺
     * 飞虎队到发型师绑定入口
     *
     * @param craftsmanShop
     * @throws Throwable
     * @author 高杰
     * @time 2018年12月26日 上午11:40:26
     */
    @Transactional
    public void modifyCraftsmanShop(CraftsmanShopTO craftsmanShop, long operator) throws Throwable {
        CraftsmanTO craftsman = craftsmanService.selectByPrimary(craftsmanShop.getCraftsmanId());
        if (craftsman == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "账户不存在");
        CraftsmanJobTO craftsmanJob = craftsmanJobService.selectByPrimary(craftsmanShop.getCraftsmanId());
        if (craftsmanJob == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "工作信息不存在");
        if (!(CraftsmanTO.Status.FLOW == craftsman.getStatus() || CraftsmanTO.Status.BIND == craftsman.getStatus()))
            throw new BusinessException(BusinessException.Type.DEFAULT, "只有飞虎队或者发型师才能绑定店铺");

        if (0 == craftsmanJob.getShopId()) {
            if (CraftsmanTO.Status.BIND != craftsman.getStatus()) {
                craftsmanService.update(craftsman.setStatus(CraftsmanTO.Status.BIND).setUpdatedBy(operator));
                craftsmanJobService.update(craftsmanJob.setFormalTime(new Date()).setShopId(craftsmanShop.getShopId()).setUpdatedBy(operator));
            }
        } else {
            //更改
            if (CraftsmanTO.Status.BIND != craftsman.getStatus()) {
                craftsmanService.update(craftsman.setStatus(CraftsmanTO.Status.BIND).setUpdatedBy(operator));
            }
            craftsmanJobService.update(craftsmanJob.setShopId(craftsmanShop.getShopId()).setUpdatedBy(operator));
        }

//        CraftsmanShopTO craftsmanShopDb = craftsmanShopService
//                .selectByCraftsmanIdAndShopId(craftsmanShop.getCraftsmanId(), craftsmanShop.getShopId());
//        if (craftsmanShopDb == null) {
//            craftsmanShopService.insert(new CraftsmanShopTO().setShopId(craftsmanShop.getShopId()).setCraftsmanId(craftsmanShop.getCraftsmanId()).setIsDelete(YesNoEnum.NO).setCreatedBy(operator));
//
//            if (CraftsmanTO.Status.BIND != craftsman.getStatus()) {
//                craftsmanService.update(craftsman.setStatus(CraftsmanTO.Status.BIND).setUpdatedBy(operator));
//                craftsmanJobService.update(craftsmanJob.setFormalTime(new Date()).setUpdatedBy(operator));
//            }
//        } else {
//            craftsmanShopService.delete(craftsmanShopDb);
//            CraftsmanShopQO craftsmanShopQO = new CraftsmanShopQO().setCraftsmanId(craftsmanShop.getCraftsmanId())
//                    .setIsDelete(YesNoEnum.NO);
//            //学员毕业考核
//            if (craftsmanShopService.countByQuery(craftsmanShopQO) < 1) {
//                craftsmanService.update(craftsman.setStatus(CraftsmanTO.Status.FLOW).setUpdatedBy(operator));
//
//                craftsmanJobService.update(craftsmanJob.setGradTime(new Date()).setUpdatedBy(operator));
//            }
//        }
    }

    /**
     * 门店下是否有发型师
     *
     * @param shopQO:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/5/10 18:09
     */
    public BaseDTO checkShopAndCraftsman(CraftsmanShopQO shopQO) throws Throwable {
        if (null == shopQO || 0 == shopQO.getShopId()) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "店铺不能为空");
        }
        BaseDTO baseDTO = new BaseDTO();
        baseDTO.setId(String.valueOf(craftsmanShopService.countByQuery(shopQO)));
        return baseDTO;
    }

    /**
     * 一线员工召回
     *
     * @param craftsmanBack
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年3月18日 下午7:27:39
     */
    @Transactional
    public void modifyCraftsmanBack(CraftsmanBackTO craftsmanBack, long operator) throws Throwable {
        CraftsmanTO craftsmanDb = craftsmanService.selectByPrimary(craftsmanBack.getCraftsmanId());
        if (craftsmanDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "员工不存在");
        if (!(craftsmanDb.getStatus() == CraftsmanTO.Status.BIND || craftsmanDb.getStatus() == CraftsmanTO.Status.FLOW))
            throw new BusinessException(BusinessException.Type.DEFAULT, "非飞虎队和发型师，不能离职");
        CraftsmanJobTO craftsmanJobDb = craftsmanJobService.selectByPrimary(craftsmanBack.getCraftsmanId());
        if (craftsmanJobDb == null)
            throw new BusinessException(BusinessException.Type.DEFAULT, "员工工作信息不存在");

        // TODO

        craftsmanDb.setStatus(CraftsmanTO.Status.BACK);
        craftsmanService.update(craftsmanDb.setUpdatedBy(operator));
        craftsmanBackService.insert(craftsmanBack);
    }

    /**
     * 获取职级列表
     *
     * @param craftsmanLevelQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午4:50:43
     */
    public List<CraftsmanLevelTO> getCraftsmanLevels(CraftsmanLevelQO craftsmanLevelQuery) throws Throwable {
        return craftsmanLevelService.selectByQuery(craftsmanLevelQuery);
    }

    /**
     * 获取职级详情
     *
     * @param craftsmanLevelQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午4:50:43
     */
    public CraftsmanLevelTO getCraftsmanLevel(CraftsmanLevelQO craftsmanLevelQuery) throws Throwable {
        return craftsmanLevelService.selectByPrimary(craftsmanLevelQuery.getId());
    }

    /**
     * 修改职级
     *
     * @param craftsmanLevel
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月12日 下午4:14:30
     */
    @Transactional
    public void modifyCraftsmanLevel(CraftsmanLevelTO craftsmanLevel, long operator) throws Throwable {
        if (null != craftsmanLevel && craftsmanLevel.getIsDelete() == YesNoEnum.YES) {
            long count = craftsmanJobService.count(new CraftsmanJobQO().setLevelId(craftsmanLevel.getId()).setIsDelete(YesNoEnum.NO));
            if (count > 0) {
                throw new BusinessException(BusinessException.Type.DEFAULT, "该岗位已经绑定在职的发型师,无法失效");
            }
        }
        craftsmanLevelService.update(craftsmanLevel.setUpdatedBy(operator));
    }

    /**
     * 新增职级
     *
     * @param craftsmanLevel
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月12日 下午4:14:35
     */
    @Transactional
    public void newCraftsmanLevel(CraftsmanLevelTO craftsmanLevel, long operator) throws Throwable {
        craftsmanLevelService.insert(craftsmanLevel.setCreatedBy(operator).setUpdatedBy(operator));
    }

    /**
     * 获取职级列表
     *
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午4:50:43
     */
    public List<CraftsmanDutyTO> getCraftsmanDutys(CraftsmanDutyQO craftsmanDutyQuery) throws Throwable {
        return craftsmanDutyService.selectByQuery(craftsmanDutyQuery);
    }

    /**
     * 获取岗位详情
     *
     * @param craftsmanDutyQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月12日 下午5:02:57
     */
    public CraftsmanDutyTO getCraftsmanDuty(CraftsmanDutyQO craftsmanDutyQuery) throws Throwable {
        return craftsmanDutyService.selectByPrimary(craftsmanDutyQuery.getId());
    }

    /**
     * 修改岗位
     *
     * @param craftsmanDuty
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月12日 下午5:02:21
     */
    @Transactional
    public void modifyCraftsmanDuty(CraftsmanDutyTO craftsmanDuty, long operator) throws Throwable {
        CraftsmanDutyTO craftsmanDutyDb = craftsmanDutyService.selectByPrimary(craftsmanDuty.getId());
        if (null == craftsmanDutyDb || craftsmanDutyDb.getSequence() < 0)
            throw new BusinessException(BusinessException.Type.DEFAULT, "该岗位不存在");

        if (craftsmanDuty.getIsDelete() == YesNoEnum.YES) {
            long count = craftsmanJobService.count(new CraftsmanJobQO().setDutyId(craftsmanDuty.getId()).setIsDelete(YesNoEnum.NO));
            if (count > 0) {
                throw new BusinessException(BusinessException.Type.DEFAULT, "该岗位已经绑定在职的发型师,无法失效");
            }
        }
        craftsmanDutyService.update(craftsmanDuty.setUpdatedBy(operator));
    }

    /**
     * 新增岗位
     *
     * @param operator
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月12日 下午5:01:26
     */
    public void newCraftsmanDuty(CraftsmanDutyTO craftsmanDuty, long operator) throws Throwable {
        craftsmanDutyService.insert(craftsmanDuty.setCreatedBy(operator).setUpdatedBy(operator));
    }

    /**
     * 获取职级列表
     *
     * @param craftsmanDutyQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午4:50:43
     */
    public List<CraftsmanDutyTO> getCraftsmanDutys(CraftsmanDutyQO craftsmanDutyQuery, Page<CraftsmanDutyTO> pageDO)
            throws Throwable {
        pageDO.init(craftsmanDutyService.count(craftsmanDutyQuery));
        craftsmanDutyQuery.setRecordStart(pageDO.getRecordStart()).setRecordEnd(pageDO.getRecordEnd());
        List<CraftsmanDutyTO> craftsmanDutyList = craftsmanDutyService.selectByQuery(craftsmanDutyQuery);
        pageDO.setDtoList(craftsmanDutyList);
        return craftsmanDutyList;
    }

    /**
     * 获取职级列表
     *
     * @param craftsmanLevelQuery
     * @return
     * @throws Throwable
     * @author 高杰
     * @time 2019年1月8日 下午4:50:43
     */
    public List<CraftsmanLevelTO> getCraftsmanLevels(CraftsmanLevelQO craftsmanLevelQuery,
                                                     Page<CraftsmanLevelTO> pageTO) throws Throwable {
        pageTO.init(craftsmanLevelService.count(craftsmanLevelQuery));
        craftsmanLevelQuery.setRecordStart(pageTO.getRecordStart()).setRecordEnd(pageTO.getRecordEnd());
        List<CraftsmanLevelTO> craftsmanLevelList = craftsmanLevelService.selectByQuery(craftsmanLevelQuery.setOrderBy("createdtime DESC"));
        pageTO.setDtoList(craftsmanLevelList);
        return craftsmanLevelList;
    }

    /**
     * 查询店铺员工姓名和数量
     *
     * @param craftsmanShopQO:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/3/30 11:26
     */
    public CraftsmanSocialTO getCraftsmanShop(CraftsmanShopQO craftsmanShopQO) throws Throwable {
        if (-1 == craftsmanShopQO.getId()) {

        } else {
            CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(craftsmanShopQO.getCraftsmanId());
            if (null == craftsmanTO || craftsmanTO.getId() <= 0) {
                throw new BusinessException(BusinessException.Type.DEFAULT, "发型师不存在");
            }
            if (craftsmanTO.getStatus() == CraftsmanTO.Status.LEAVE) {
                throw new BusinessException(BusinessException.Type.DEFAULT, "发型师已经离职");
            }
        }
//        craftsmanShopQO.setCraftsmanId(0);
//        List<CraftsmanShopTO> shopList = craftsmanShopService.selectByQuery(craftsmanShopQO.setIsDelete(YesNoEnum.NO));

        //工作信息
        List<CraftsmanJobTO> jobTOList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setShopId(craftsmanShopQO.getShopId()).setIsDelete(YesNoEnum.NO));
        if (null == jobTOList || jobTOList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "该门店暂未绑定发型师");
        }
        List<Long> ids = jobTOList.stream().map(a -> a.getId()).collect(Collectors.toList());
        Set<CraftsmanTO.Status> set = new HashSet<>();
        set.add(CraftsmanTO.Status.BIND);
        set.add(CraftsmanTO.Status.STUDENT);
        set.add(CraftsmanTO.Status.FLOW);
        set.add(CraftsmanTO.Status.BACK);
        List<CraftsmanTO> craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(ids).setStatuss(set).setIsDelete(YesNoEnum.NO));
        if (null == craftsmanList || craftsmanList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师为空");
        }
        List<Long> craftsmanIds = craftsmanList.stream().map(a -> a.getId()).collect(Collectors.toList());
        List<CraftsmanSocialTO> socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(craftsmanIds).setIsDelete(YesNoEnum.NO));
        if (null == socialTOList || socialTOList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师信息为空");
        }
        StringBuilder sb = new StringBuilder();
        for (CraftsmanSocialTO socialTO : socialTOList) {
            sb.append(socialTO.getName());
            sb.append(" ");
        }
        return new CraftsmanSocialTO().setId(socialTOList.size()).setName(sb.toString());
    }

    /**
     * 根据shopid,type 查询用户url
     *
     * @param type:
     * @return : java.util.List<com.xingkeduo.pojo.table.CraftsmanFileTO>
     * @author : gaozifeng
     * @date : 2019/4/11 17:21
     */
    public List<CraftsmanFileDTO> getShopCraftsmanInfo(List<Long> craftsmanIds, String type) throws Throwable {
        if (null == craftsmanIds || craftsmanIds.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师不能为空");
        }
        if (StringUtils.isEmpty(type)) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "类型不能为空");
        }

        Set<CraftsmanTO.Status> set = new HashSet<>();
        set.add(CraftsmanTO.Status.BIND);
        set.add(CraftsmanTO.Status.STUDENT);
        set.add(CraftsmanTO.Status.FLOW);
        set.add(CraftsmanTO.Status.BACK);
        List<CraftsmanTO> craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(craftsmanIds).setIsDelete(YesNoEnum.NO).setStatuss(set));
        if (null == craftsmanList || craftsmanList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "没有查询到发型师");
        }

        List<CraftsmanFileTO> fileList = craftsmanFileService.selectForList(new CraftsmanFileQO().setIds(craftsmanIds).setType(type).setIsDelete(YesNoEnum.NO));
        if (null == fileList || fileList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "没有数据");
        }

        CraftsmanJobQO craftsmanJobQO = new CraftsmanJobQO();
        craftsmanJobQO.setIds(craftsmanIds).setIsDelete(YesNoEnum.NO);
        List<CraftsmanJobTO> craftsmanJobTOS = craftsmanJobService.selectByQueryForShopId(craftsmanJobQO);
        if (null == craftsmanList || craftsmanList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "数据不存在");
        }
        Map<Long, CraftsmanTO> map = craftsmanList.stream().collect(Collectors.toMap(CraftsmanTO::getId, a -> a, (k1, k2) -> k1));
        //文件
        Map<Long, CraftsmanFileTO> fileMap = fileList.stream().collect(Collectors.toMap(CraftsmanFileTO::getJobseeker_id, a -> a, (k1, k2) -> k1));
        //名字
        List<CraftsmanSocialTO> socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(craftsmanIds).setIsDelete(YesNoEnum.NO));
        Map<Long, CraftsmanSocialTO> socialMap = socialTOList.stream().collect(Collectors.toMap(CraftsmanSocialTO::getId, a -> a, (k1, k2) -> k1));

        List<CraftsmanFileDTO> returnList = new LinkedList<>();
        for (CraftsmanJobTO jobTO : craftsmanJobTOS) {
            CraftsmanFileDTO dto = new CraftsmanFileDTO();
            long id = jobTO.getId();
            CraftsmanFileTO fileTO = fileMap.get(id);
            CraftsmanTO craftsmanTO = map.get(id);
            if (null == craftsmanTO) {
                continue;
            }
            if (craftsmanTO.getStatus() == CraftsmanTO.Status.LEAVE) {
                continue;
            }
            CraftsmanSocialTO socialTO = socialMap.get(id);
            //工号,id,艺名
            if (null != craftsmanTO) {
                dto.setId(craftsmanTO.getId());
                dto.setCode(craftsmanTO.getCode()).setJobseeker_id(craftsmanTO.getId());
            }
            if (null != socialTO) {
                dto.setName(socialTO.getName() != null ? socialTO.getName() : "");
                dto.setCraftsmanName(jobTO.getNickName() != null ? jobTO.getNickName() : dto.getName());
            }
            if (null != fileTO && null != fileTO.getPath()) {
                dto.setPath(fileTO.getPath());
            } else {
            }
            returnList.add(dto);
        }

        return returnList;

    }


    /**
     * 外部接口   查询发型师信息,code
     *
     * @param craftsmanQuery:
     * @return : com.xingkeduo.pojo.table.CraftsmanSocialTO
     * @author : gaozifeng
     * @date : 2019/4/11 20:30
     */
    public CraftsmanSocialTO getCraftsmanNameForCode(CraftsmanQO craftsmanQuery) throws Throwable {
        if (craftsmanQuery.getCode() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "工号不能为空");
        }
        Set<CraftsmanTO.Status> set = new HashSet<>();
        set.add(CraftsmanTO.Status.BIND);
        set.add(CraftsmanTO.Status.STUDENT);
        set.add(CraftsmanTO.Status.FLOW);
        set.add(CraftsmanTO.Status.BACK);
        List<CraftsmanTO> craftsmanTOList = craftsmanService.selectByQuery(craftsmanQuery.setStatuss(set).setIsDelete(YesNoEnum.NO));
        log.info("craftsmanTOList----------" + craftsmanTOList.size());
        if (null == craftsmanTOList || craftsmanTOList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "未找到在职发型师");
        }
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(craftsmanTOList.get(0).getId());
        socialTO.setCode(craftsmanTOList.get(0).getCode());
        log.info("socialTO----------" + socialTO.toString());
        if (null == socialTO || socialTO.getId() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "未找到在职发型师");
        }
        return socialTO;
    }


    /**
     * 外部接口   查询发型师信息,code    离职也查
     *
     * @param craftsmanQuery:
     * @return : com.xingkeduo.pojo.table.CraftsmanSocialTO
     * @author : gaozifeng
     * @date : 2019/4/11 20:30
     */
    public List<Long> getCraftsmanIdByCondition(CraftsmanQO craftsmanQuery, CraftsmanJobQO jobQO, CraftsmanSocialQO socialQO) throws Throwable {
        if (0L == craftsmanQuery.getCode() && StringUtils.isBlank(jobQO.getNickName()) && StringUtils.isBlank(socialQO.getName())) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "搜索条件不能为空");
        }

        List<Long> list = new LinkedList<>();
        List<Long> social = null;

        if (StringUtils.isNotBlank(jobQO.getNickName())) {
            list = craftsmanJobService.selectPrimatyByQuery(jobQO);
        }

        if (StringUtils.isNotBlank(socialQO.getName())) {
            if (null == list) {
                list = craftsmanSocialService.selectPrimaryByQuery(socialQO.setIsDelete(YesNoEnum.NO));
            } else {
                social = craftsmanSocialService.selectPrimaryByQuery(socialQO.setIsDelete(YesNoEnum.NO));
                list.addAll(social);
            }
        }
        if (0L != craftsmanQuery.getCode()) {
            List<CraftsmanTO> craftsman = craftsmanService.selectByQuery(craftsmanQuery.setIsDelete(YesNoEnum.NO));
            if (null != craftsman && craftsman.size() > 0) {
                list.add(craftsman.get(0).getId());
            }
        }

        if (null != list && list.size() > 0) {
            List<CraftsmanTO> craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(list).setIsDelete(YesNoEnum.NO));
            if (null != craftsmanList && craftsmanList.size() > 0) {
                List<Long> craftsmanIds = craftsmanList.stream().map(a -> a.getId()).collect(Collectors.toList());
                log.info("return craftsmanList{}---->", JSON.toJSONString(craftsmanIds));
                return craftsmanIds;
            }
        }
        return new LinkedList<>();
    }

    /**
     * 外部接口 根据id 返回头像，个签，id，艺名
     *
     * @param craftsmanQO:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/7 21:07
     */
    public UserDTO getCraftsmanForId(CraftsmanQO craftsmanQO) throws Throwable {
        CraftsmanTO craftsmanById = getCraftsmanById(craftsmanQO.getId());
        CraftsmanJobTO craftsmanJobById = getCraftsmanJobById(craftsmanQO.getId());
        CraftsmanSocialTO craftsmanSocialById = getCraftsmanSocialById(craftsmanQO.getId());
        List<CraftsmanFileTO> craftsmanFileTOList = craftsmanFileService.selectForList(new CraftsmanFileQO().setJobseeker_id(craftsmanById.getId()).setType("1").setIsDelete(YesNoEnum.NO));
        CraftsmanFileTO fileTO = null;
        if (null == craftsmanFileTOList || craftsmanFileTOList.size() <= 0) {

        } else {
            fileTO = craftsmanFileTOList.stream().findFirst().orElse(null);
        }

        UserDTO dto = new UserDTO();
        dto.setId(craftsmanById.getId()).setName(craftsmanJobById.getNickName() == null ? craftsmanSocialById.getName() : craftsmanJobById.getNickName());
        dto.setCode(craftsmanById.getCode());
//                .setPath(fileTO.getPath() == null ? null : fileTO.getPath()).setSignaTure(craftsmanSocialById.getSignaTure() == null ? "" : craftsmanSocialById.getSignaTure());
        if (null != fileTO) {
            dto.setPath(fileTO.getPath() == null ? "" : fileTO.getPath());
        }
        if (null != craftsmanSocialById.getSignaTure()) {
            dto.setSignaTure(craftsmanSocialById.getSignaTure() == null ? "" : craftsmanSocialById.getSignaTure());
        }
        if (StringUtils.isBlank(dto.getName())) {
            dto.setName(craftsmanSocialById.getName() != null ? craftsmanSocialById.getName() : "小星星");
        }
        return dto;
    }

    /**
     * 获取经理门店列表
     *
     * @param craftsmanQO:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/4/28 10:59
     */
    public List<BaseDTO> getCraftsmanShopList(CraftsmanQO craftsmanQO) throws Throwable {
        log.info("shop 请求信息----------" + craftsmanQO.toString());
        log.info("shop 请求信息----------" + craftsmanQO.getId());
        CraftsmanTO craftsmanTO = getCraftsmanById(craftsmanQO.getId());
        CraftsmanJobTO jobTO = getCraftsmanJobById(craftsmanTO.getId());
        CraftsmanDutyTO craftsmanDutyTO = craftsmanDutyService.selectByPrimary(jobTO.getDutyId());
        if (null == craftsmanDutyTO || craftsmanDutyTO.getSequence() <= 1) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "不是经理权限");
        }
        List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setCraftsmanId(craftsmanTO.getId()).setIsDelete(YesNoEnum.NO));
        if (null == shopTOList || shopTOList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "没有查询到门店信息");
        }
        String encode = JsonUtils.encodeParam("ids", shopTOList.stream().map(a -> a.getShopId()).collect(Collectors.toList()));
        List<BaseDTO> shopForList = feignDomain.getShopForList(encode);
        log.info("shop 请求信息   --shopForList.size()--------" + shopForList.size());
        return shopForList;
    }

    /**
     * 根据店铺id集合查询所有发型师 签约过的店铺  存在飞虎队/学员是签约的情况
     *
     * @param shopIds:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/29 17:42
     */
    public List<BaseDTO> getShopIdsByServiceCraftsman(List<Long> shopIds) throws Throwable {
        if (null == shopIds || shopIds.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "店铺数据不能为空");
        }

        List<Long> jobList = craftsmanJobService.selectPrimatyByQuery(new CraftsmanJobQO().setShopIds(shopIds).setIsDelete(YesNoEnum.NO));
        if (null == jobList || jobList.size() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "没有查询到发型师");
        }
        Set<CraftsmanTO.Status> craftsmanTypeSet = new HashSet<>();
        craftsmanTypeSet.add(CraftsmanTO.Status.BIND);
        craftsmanTypeSet.add(CraftsmanTO.Status.STUDENT);
        craftsmanTypeSet.add(CraftsmanTO.Status.FLOW);
        craftsmanTypeSet.add(CraftsmanTO.Status.BACK);
        List<Long> craftsmanIds = craftsmanService.selectById(new CraftsmanQO().setStatuss(craftsmanTypeSet).setIsDelete(YesNoEnum.NO).setIds(jobList));

        List<CraftsmanJobTO> jobTOList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setShopIds(shopIds).setIds(craftsmanIds).setIsDelete(YesNoEnum.NO));
        List<CraftsmanSocialTO> socialList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(craftsmanIds));

        Map<Long, CraftsmanSocialTO> socialMap = socialList.stream().collect(Collectors.toMap(CraftsmanSocialTO::getId, a -> a, (k1, k2) -> k1));

        List<BaseDTO> returnList = new LinkedList<>();
        for (int i = 0; i < shopIds.size(); i++) {
            List<CraftsmanSocialTO> craftsmanInfo = new LinkedList<>();
            BaseDTO dto = new BaseDTO();
            //门店数据
            for (int j = 0; j < jobTOList.size(); j++) {
                if (shopIds.get(i) == jobTOList.get(j).getShopId()) {
                    dto.setId(String.valueOf(shopIds.get(i)));
                    craftsmanInfo.add(socialMap.get(jobTOList.get(j).getId()));
                }
            }
            dto.setSocialList(craftsmanInfo);
            returnList.add(dto);
        }
        return returnList;
    }


    /**
     * 根据门店id和 发型师id 查询出 服务价格
     *
     * @param serviceQO:
     * @param craftsmanIds:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/29 16:35
     */
    public BaseDTO getCraftsmanServiceList(CraftsmanServiceQO serviceQO, List<Long> craftsmanIds) throws Throwable {
        if (0L == serviceQO.getShopId()) {
            throw new DataException(DataException.Type.DEFAULT, "门店信息不能为空");
        }
        //发型师服务 和 价格
        List<CraftsmanServiceTO> shopCraftsmanList = craftsmanServiceService.selectByQuery(serviceQO.setCraftsmanIds(craftsmanIds).setIsDelete(YesNoEnum.NO).setStatus(YesNoEnum.NO));

        BaseDTO dto = new BaseDTO();
        //外部
        //发型师和 服务集合DTO
        List<ShopCraftsmanServiceDto> serviceDto = new LinkedList<>();
        for (Long cids : craftsmanIds) {
            //数据库服务集合遍历 servicePriceList
            ShopCraftsmanServiceDto craftsmanServiceDto = new ShopCraftsmanServiceDto();
            //服务集合  serviceList
            List<CraftsmanServiceTO> serviceList = new LinkedList<>();
            for (CraftsmanServiceTO serviceTO : shopCraftsmanList) {
                if (serviceTO.getCraftsmanId() == cids) {
                    //发型师id
                    craftsmanServiceDto.setCraftsmanId(serviceTO.getCraftsmanId());
                    //商店id
                    dto.setShopId(serviceTO.getShopId());
                    //serviceList 多个服务集合
                    serviceList.add(serviceTO);
                }
                //设置到 服务集合
                craftsmanServiceDto.setServiceList(serviceList);
            }
            if (0L == craftsmanServiceDto.getCraftsmanId()) {
                craftsmanServiceDto.setCraftsmanId(cids);
            }
            serviceDto.add(craftsmanServiceDto);
            //外部发型师服务集合
        }
        dto.setShopCraftsmanServiceDtos(serviceDto);
        return dto;
    }


    /**
     * 店铺更改技能 替换所有服务技能
     *
     * @param craftsmanServiceQO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/6/6 10:33
     */
    @Transactional
    public void shopUpdateService(CraftsmanServiceQO craftsmanServiceQO) throws Throwable {
        if (null == craftsmanServiceQO && craftsmanServiceQO.getShopId() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "门店不能为空");
        }
        if (null == craftsmanServiceQO.getServiceIds() || craftsmanServiceQO.getServiceIds().size() <= 0) {
            throw new DataException(DataException.Type.DEFAULT, "服务技能不能为空");
        }
        //店铺技能更改,把所有发型师的技能都delete=1  店铺再更改时 不再生效
        List<CraftsmanServiceTO> serviceList = craftsmanServiceService.selectByQuery(craftsmanServiceQO.setIsDelete(YesNoEnum.NO));
        log.info("查询出需要更改的发型师技能---" + JSON.toJSONString(serviceList));
        if (null != serviceList && serviceList.size() > 0) {
            List<CraftsmanServiceTO> updateList = serviceList.stream().map(a -> a.setIsDelete(YesNoEnum.YES).setStuatus(YesNoEnum.YES)).collect(Collectors.toList());
            int retVal = craftsmanServiceService.updateList(updateList);
            if (retVal != updateList.size()) {
                throw new DataException(DataException.Type.DEFAULT, "服务技能不能为空");
            }
        } else {
            log.info("没有到需要更改的发型师技能");
        }
    }


    /**
     * 发型师服务技能/调价
     *
     * @param craftsmanTO:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/5/6 14:39
     */
    private void craftsmanGetService(CraftsmanTO craftsmanTO) {
        if (craftsmanTO.getStatus() != CraftsmanTO.Status.FLOW && craftsmanTO.getStatus() != CraftsmanTO.Status.BIND && craftsmanTO.getStatus() != CraftsmanTO.Status.STUDENT) {
            throw new DataException(DataException.Type.DEFAULT, "无法配置发型师服务技能");
        }
    }

    public CraftsmanTO getCraftsmanById(long id) throws Throwable {
        CraftsmanTO craftsmanTO = craftsmanService.selectByPrimary(id);
        if (null == craftsmanTO || craftsmanTO.getId() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "未找到在职发型师");
        }
        return craftsmanTO;
    }

    public CraftsmanJobTO getCraftsmanJobById(long id) throws Throwable {
        CraftsmanJobTO job = craftsmanJobService.selectByPrimary(id);
        if (null == job || job.getId() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "未找到在职发型师职位信息");
        }
        return job;
    }

    public CraftsmanSocialTO getCraftsmanSocialById(long id) throws Throwable {
        CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(id);
        if (null == socialTO || socialTO.getId() <= 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "未找到在职发型师职位信息");
        }
        return socialTO;
    }


    /**
     * 外部接口，内部可用，发型师id，名称，艺名
     *
     * @return : com.xingkeduo.pojo.dto.BaseDTO
     * @author : gaozifeng
     * @date : 2019/5/13 20:24
     */
    public List<BaseDTO> getCraftsmanInfo(CraftsmanQO craftsmanQO, CraftsmanJobQO jobQO, CraftsmanSocialQO socialQO) throws Throwable {
        //TODO 待优化
        List<BaseDTO> baseDTOList = new ArrayList<>(initListSize);

        List<Long> ids = craftsmanJobService.selectPrimatyByQuery(jobQO);
        List<Long> socialList = craftsmanSocialService.selectPrimaryByQuery(socialQO);

        ids.addAll(socialList);

        Set<CraftsmanTO.Status> set = new HashSet<>();
        set.add(CraftsmanTO.Status.BIND);
        set.add(CraftsmanTO.Status.STUDENT);
        set.add(CraftsmanTO.Status.FLOW);
        set.add(CraftsmanTO.Status.BACK);
        List<CraftsmanTO> craftsmanTOS = craftsmanService.selectByQuery(craftsmanQO.setStatuss(set).setIds(ids).setIsDelete(YesNoEnum.NO));

        log.info("查询非离职发型师");
        List<Long> craftsmanIds = craftsmanTOS.stream().map(a -> a.getId()).collect(Collectors.toList());
        log.info("非离职发型师ids" + JSON.toJSONString(craftsmanIds));
        ids.retainAll(craftsmanIds);

        //发型师工作id集合
        CraftsmanJobQO craftsmanJobQO = new CraftsmanJobQO();
        craftsmanJobQO.setIds(ids);
        List<CraftsmanJobTO> jobTOList = craftsmanJobService.selectByQueryForShopId(craftsmanJobQO.setIsDelete(YesNoEnum.NO));

        //查询发型师
        List<CraftsmanSocialTO> craftsmanSocialTOS = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(ids).setIsDelete(YesNoEnum.NO));

        Map<Long, CraftsmanTO> craftsmanMap = craftsmanTOS.stream().collect(Collectors.toMap(CraftsmanTO::getId, a -> a, (k1, k2) -> k1));

        Map<Long, CraftsmanJobTO> jobMap = jobTOList.stream().collect(Collectors.toMap(CraftsmanJobTO::getId, a -> a, (k1, k2) -> k1));
        List<CraftsmanSocialTO> list = craftsmanSocialTOS.stream().peek(a -> {
            CraftsmanJobTO jobTO = jobMap.get(a.getId());
            log.info("工作信息:-------" + JSON.toJSONString(jobTO));

            if (null != jobTO) {
                if (null != jobTO.getNickName()) {
                    //艺名
                    a.setWechatName(jobTO.getNickName());
                    //店铺
                    log.info("店铺ID:-------" + jobTO.getShopId());
                    a.setCode(jobTO.getShopId() != 0 ? jobTO.getShopId() : 0);
                }
            }
        }).collect(Collectors.toList());

        for (int i = 0, j = craftsmanSocialTOS.size(); i < j; i++) {
            BaseDTO baseDTO = new BaseDTO();
            long id = list.get(i).getId();

            baseDTO.setId(String.valueOf(id)).setValue(list.get(i).getWechatName()).setShopId(list.get(i).getCode()).setName(list.get(i).getName());
            CraftsmanTO craftsmanTO = craftsmanMap.get(id);
            log.info("获取发型师信息" + JSON.toJSONString(craftsmanTO));
            if (null != craftsmanTO && craftsmanTO.getId() > 0) {
                baseDTO.setCode(String.valueOf(craftsmanTO.getCode()));
            }
            baseDTOList.add(baseDTO);
        }
        return baseDTOList;
    }

    /**
     * 获取经理和员工数量
     * 经理可能管理 签约店铺  可能没有管理签约店铺
     *
     * @param shopQO:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/14 14:38
     */
    public CraftsmanShopDTO getManagerName(CraftsmanShopQO shopQO) throws Throwable {
        //返回信息
        CraftsmanShopDTO paramInfo = new CraftsmanShopDTO();
        //发型师信息
        List<CraftsmanJobTO> hairList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setShopId(shopQO.getShopId()));

        Map<Long, CraftsmanJobTO> jobMap = hairList.stream().collect(Collectors.toMap(CraftsmanJobTO::getId, a -> a, (k1, k2) -> k1));

        List<Long> craftsmanIds = hairList.stream().map(a -> a.getId()).collect(Collectors.toList());

        //店经理
        List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(shopQO.setIsDelete(YesNoEnum.NO));
        CraftsmanTO craftsmanTO = null;
        if (null == shopTOList || shopTOList.size() <= 0) {
//            throw new BusinessException(BusinessException.Type.DEFAULT, "店经理不存在");
        } else {
            //店经理信息
            List<CraftsmanJobTO> managerList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setId(shopTOList.get(0).getCraftsmanId()));
            if (null == managerList || managerList.size() <= 0) {
                throw new BusinessException(BusinessException.Type.DEFAULT, "店经理信息不存在");
            }

            //经理
            craftsmanTO = craftsmanService.selectByPrimary(managerList.get(0).getId());
            if (null != craftsmanTO && CraftsmanTO.Status.LEAVE != craftsmanTO.getStatus()) {
                CraftsmanDTO craftsmanDTO = new CraftsmanDTO();
                craftsmanDTO.setId(craftsmanTO.getId());
                craftsmanDTO.setCode(craftsmanTO.getCode());
                CraftsmanJobTO jobTO = jobMap.get(craftsmanTO.getId());
                if (null != jobTO && null != jobTO.getNickName()) {
                    craftsmanDTO.setNickName(jobTO.getNickName());
                }
                CraftsmanSocialTO socialTO = craftsmanSocialService.selectByPrimary(craftsmanTO.getId());
                if (null != socialTO && null != socialTO.getName()) {
                    craftsmanDTO.setName(socialTO.getName());
                }
                paramInfo.setCraftsman(craftsmanDTO);
//                paramInfo.setCount(1);
            }

            //店经理信息
//            CraftsmanJobTO managerJobInfo = managerList.get(0);
        }

        //查询发型师
        List<CraftsmanTO> craftsmanList;
        if (craftsmanIds.size() <= 0) {
            log.info("user  ---manager    该店铺下未发现发型师");
        } else {
            Set<CraftsmanTO.Status> set = new HashSet<>();
            set.add(CraftsmanTO.Status.BIND);
            set.add(CraftsmanTO.Status.STUDENT);
            set.add(CraftsmanTO.Status.FLOW);
            set.add(CraftsmanTO.Status.BACK);
            craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(craftsmanIds).setStatuss(set).setIsDelete(YesNoEnum.NO));
            List<Long> ids = craftsmanList.stream().map(a -> a.getId()).collect(Collectors.toList());
            //所有发型师艺名
            List<CraftsmanSocialTO> socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(ids));
            Map<Long, CraftsmanSocialTO> socialMap = socialTOList.stream().collect(Collectors.toMap(CraftsmanSocialTO::getId, a -> a, (k1, k2) -> k1));
            //员工
            List<CraftsmanDTO> staffList = new LinkedList<>();
            for (CraftsmanTO craftsman : craftsmanList) {
                CraftsmanDTO staffInfo = new CraftsmanDTO();
                CraftsmanJobTO jobTO = jobMap.get(craftsman.getId());
                staffInfo.setId(craftsman.getId());
                if (null != jobTO) {
                    staffInfo.setNickName(jobTO.getNickName());
                }
                staffInfo.setCode(craftsman.getCode());
                CraftsmanSocialTO socialTO = socialMap.get(craftsman.getId());
                log.info("外部接口user ---- getmanagerName ---发型师名称-----" + JSON.toJSONString(socialTO));
                if (null != socialTO) {
                    staffInfo.setName(socialTO.getName());
                }
                staffList.add(staffInfo);
                //如果经理的id = 员工id
            }
            paramInfo.setCount(staffList.size());
            paramInfo.setCraftsmanList(staffList);
        }
        log.info("user getmanagerName -- 返回信息" + JSON.toJSONString(paramInfo));
        return paramInfo;
    }

    /**
     * 根据店铺id 获取经理信息，总经理信息
     *
     * @return : java.util.List<com.xingkeduo.pojo.dto.CraftsmanDTO>
     * @author : gaozifeng
     * @date : 2019/7/12 18:18
     */
    public CraftsmanShopDTO getManagerList(Long companyId, Long shopId) throws Throwable {
        //TODO 记录
        if (null == companyId || null == shopId) {
            log.info("根据店铺id 获取经理信息，总经理信息");
            throw new InputParamException(InputParamException.Type.EMPTY, "查询店铺信息参数");
        }
        List<CraftsmanShopTO> shopTOList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setShopId(shopId).setIsDelete(YesNoEnum.NO));
        if (null == shopTOList || shopTOList.size() == 0) {
            throw new DataException(DataException.Type.NULL, "店铺");
        }
        CraftsmanShopTO shopTO = shopTOList.get(0);

        List<CraftsmanJobTO> companyList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setDutyId(DutyEnums.COMPANY.getSequence()).setCompanyId(companyId).setIsDelete(YesNoEnum.NO));
        if (null == companyList || companyList.size() == 0) {
            throw new DataException(DataException.Type.NULL, "分公司负责人");
        }

//        Optional<CraftsmanTO> craftsmanTO = craftsmanService.selectByPrimary(shopTO.getShopId());
//        if(craftsmanTO.isPresent()){
//            craftsmanTO.get();
//        }
        CraftsmanTO managerInfo = craftsmanService.selectByPrimary(shopTO.getCraftsmanId());
        if (managerInfo.getStatus() == CraftsmanTO.Status.LEAVE || managerInfo.getIsDelete() == YesNoEnum.YES) {
            throw new DataException(DataException.Type.NULL, "店经理已离职");
        }
        CraftsmanJobTO managerJobTO = craftsmanJobService.selectByPrimary(managerInfo.getId());
        CraftsmanSocialTO managerSocialTO = craftsmanSocialService.selectByPrimary(managerInfo.getId());

        CraftsmanShopDTO dto = new CraftsmanShopDTO();
        CraftsmanDTO manager = new CraftsmanDTO();
        manager.setId(managerInfo.getId());
        manager.setName(Optional.ofNullable(managerSocialTO.getName()).orElse(""));
        manager.setNickName(Optional.ofNullable(managerJobTO.getNickName()).orElse(""));
        manager.setPhone(Optional.ofNullable(managerSocialTO.getPhone()).orElse(""));

        List<CraftsmanDTO> companyDTOList;
        List<Long> ids = companyList.stream().map(BaseTable::getId).collect(Collectors.toList());
        Set<CraftsmanTO.Status> set = new HashSet<>();
        set.add(CraftsmanTO.Status.BIND);
        set.add(CraftsmanTO.Status.STUDENT);
        set.add(CraftsmanTO.Status.FLOW);
        set.add(CraftsmanTO.Status.BACK);
        List<CraftsmanTO> craftsmanList = craftsmanService.selectByQuery(new CraftsmanQO().setIds(ids).setStatuss(set).setIsDelete(YesNoEnum.NO));
        if (null == craftsmanList || craftsmanList.size() == 0) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "总经理为空");
        }
        List<Long> craftsmanIds = craftsmanList.stream().map(BaseTable::getId).collect(Collectors.toList());

        List<CraftsmanJobTO> jobTOList = craftsmanJobService.selectByQueryForShopId(new CraftsmanJobQO().setIds(craftsmanIds).setDutyId(DutyEnums.COMPANY.getSequence()).setIsDelete(YesNoEnum.NO));
        List<CraftsmanSocialTO> socialTOList = craftsmanSocialService.selectByQuery(new CraftsmanSocialQO().setIds(craftsmanIds).setIsDelete(YesNoEnum.NO));

        List<CraftsmanDTO> collect = jobTOList.stream().map(craftsmanTO -> {
            return new CraftsmanDTO(craftsmanTO.getId());

        }).peek(craftsmanDTO -> {
            jobTOList.stream().filter(jobTO -> jobTO.getId() == craftsmanDTO.getId()).findFirst().ifPresent(jobTO -> {
                craftsmanDTO.setNickName(jobTO.getNickName());
            });
            socialTOList.stream().filter(socialTO -> socialTO.getId() == craftsmanDTO.getId()).findFirst().ifPresent(socialTO -> {
                craftsmanDTO.setName(socialTO.getName()).setPhone(socialTO.getPhone());
            });
        }).collect(Collectors.toList());


//        List<CraftsmanDTO> collect = craftsmanList.stream().map(craftsmanTO -> {
//            return new CraftsmanDTO(craftsmanTO.getId());
//
//        }).peek(craftsmanDTO -> {
//            jobTOList.stream().filter(jobTO -> jobTO.getId() == craftsmanDTO.getId()).findFirst().ifPresent(jobTO -> {
//                craftsmanDTO.setNickName(jobTO.getNickName());
//            });
//            socialTOList.stream().filter(socialTO -> socialTO.getId() == craftsmanDTO.getId()).findFirst().ifPresent(socialTO -> {
//                craftsmanDTO.setName(socialTO.getName()).setPhone(socialTO.getPhone());
//            });
//        }).collect(Collectors.toList());
        dto.setCraftsman(manager);
        dto.setCraftsmanList(collect);
        return dto;
    }

    /**
     * 查询可用店铺   店经理绑定可用店铺
     *
     * @param :
     * @return : java.util.List<java.lang.Long>
     * @author : gaozifeng
     * @date : 2019/5/31 17:57
     */
    public List<BaseDTO> checkCanUseShopList(String id) throws Throwable {
        //所有门店
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(BusinessException.Type.DEFAULT, "发型师id 不能为空");
        }
        String canShopStatus = "1,2,3";
        List<BaseDTO> shopList = feignDomain.checkCanShop(canShopStatus);
        List<Long> canShopList;
        //已用门店
        List<CraftsmanShopTO> useShopList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setIsDelete(YesNoEnum.NO));
        if (null == useShopList || useShopList.size() <= 0) {
            return shopList;
        }
        log.info("已用门店长度----" + useShopList.size());
        //自己的门店 用于回显
        List<CraftsmanShopTO> craftsmanShopList = craftsmanShopService.selectByQuery(new CraftsmanShopQO().setCraftsmanId(Long.parseLong(id)).setIsDelete(YesNoEnum.NO));
        log.info("自己门店长度----" + craftsmanShopList.size());

        Map<Long, CraftsmanShopTO> myMap = craftsmanShopList.stream().collect(Collectors.toMap(CraftsmanShopTO::getShopId, a -> a, (k1, k2) -> k1));

        for (CraftsmanShopTO shopTO : useShopList) {
            Iterator<BaseDTO> it = shopList.iterator();
            while (it.hasNext()) {
                BaseDTO dto = it.next();
                //删除
                if (String.valueOf(shopTO.getShopId()).equals(dto.getId())) {
                    CraftsmanShopTO myShop = myMap.get(shopTO.getShopId());
                    if (null != myShop && shopTO.getShopId() == myShop.getShopId()) {

                    } else {
                        it.remove();
                    }
                }
            }
        }

        log.info("user ---- checkCanUseShopList ----可用店铺id集合" + shopList.size());

        return shopList;
    }

}
